package org.jeecg.modules.crawl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.crawl.entity.AllCustsale;
import org.jeecg.modules.crawl.mapper.AllCustsaleMapper;
import org.jeecg.modules.crawl.service.IAllCustsaleService;
import org.jeecg.modules.project.entity.*;
import org.jeecg.modules.project.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.List;

/**
 * @Description: 客户销售信息表
 * @Author: jeecg-boot
 * @Date:   2025-09-26
 * @Version: V1.0
 */
@Service
public class AllCustsaleServiceImpl extends ServiceImpl<AllCustsaleMapper, AllCustsale> implements IAllCustsaleService {
    @Autowired
    private IPurchasingAgentService purchasingAgentService;
    @Autowired
    private IExpertService expertService;
    @Autowired
    private IAgencyService agencyService;
    @Autowired
    private IProjectService projectService;
    @Autowired
    private IProjectResultService projectResultService;

    @Override
    public void test() {
        //采购人信息
//        List<PurchasingAgent> purchasingAgentList = baseMapper.getPurchasingAgentList();
//        if (purchasingAgentList != null && !purchasingAgentList.isEmpty()) {
//            for (PurchasingAgent purchasingAgent : purchasingAgentList) {
//                PurchasingAgent agent = purchasingAgentService.getOne(new QueryWrapper<PurchasingAgent>().eq("name", purchasingAgent.getName()).eq("area", purchasingAgent.getArea()));
//                if (agent == null) {
//                    purchasingAgentService.save(purchasingAgent);
//                }
//            }
//        }

        //专家信息
//        List<Expert> expertList = baseMapper.getExpertList();
//        if (expertList != null && !expertList.isEmpty()) {
//            for (Expert expert : expertList) {
//                Expert agent = expertService.getOne(new QueryWrapper<Expert>().eq("name", expert.getName()));
//                if (agent == null) {
//                    expertService.save(expert);
//                }
//            }
//        }

        //代理公司
//        List<Agency> agencyList = baseMapper.getAgencyList();
//        if (agencyList != null && !agencyList.isEmpty()) {
//            for (Agency expert : agencyList) {
//                if (StringUtils.isNotBlank(expert.getName())) {
//                    Agency agent = agencyService.getOne(new QueryWrapper<Agency>().eq("name", expert.getName()));
//                    if (agent == null) {
//                        agencyService.save(expert);
//                    }
//                }
//            }
//        }

        List<AllCustsale> list = list();
        if (list != null && !list.isEmpty()) {
            for (AllCustsale allCustsale : list) {
                //商机管理信息
                Project project = new Project();
                project.setProjectNum(allCustsale.getPronumbymain());
                project.setProjectName(allCustsale.getPronamebymain());
                project.setPubDate(allCustsale.getReleasedate());
                project.setUrl(allCustsale.getOriginalurlbymain());
                project.setType(allCustsale.getItem());
                project.setBudget(allCustsale.getMoney());

                if (StringUtils.isNotBlank(allCustsale.getPurchasname())) {
                    PurchasingAgent purchasingAgent = purchasingAgentService.getOne(new QueryWrapper<PurchasingAgent>().eq("name", allCustsale.getPurchasname()).eq("area", allCustsale.getArea()));
                    if (purchasingAgent == null) {
                        purchasingAgent = new PurchasingAgent();
                        purchasingAgent.setName(allCustsale.getPurchasname());
                        purchasingAgent.setArea(allCustsale.getArea());
                        purchasingAgentService.save(purchasingAgent);
                    }
                    project.setPurchasingAgentId(purchasingAgent.getId());
                    project.setPurchasingAgentName(allCustsale.getPurchasname());
                }

                if (StringUtils.isNotBlank(allCustsale.getCompbid())){
                    Agency agent = agencyService.getOne(new QueryWrapper<Agency>().eq("name", allCustsale.getCompbid()));
                    if (agent == null) {
                        agent = new Agency();
                        agent.setName(allCustsale.getCompbid());
                        agencyService.save(agent);
                    }
                    project.setAgencyId(agent.getId());
                    project.setAgencyName(agent.getName());
                }


                projectService.save(project);


                //结果信息
                if (StringUtils.isNotBlank(allCustsale.getContnum())){
                    ProjectResult projectResult = new ProjectResult();
                    projectResult.setProjectId(project.getId());
                    projectResult.setName(allCustsale.getWincompany());
                    projectResult.setPrice(allCustsale.getWinamount());
                    projectResult.setItemName(allCustsale.getItem());
                    projectResult.setUrl(allCustsale.getUrlbyresult());

                    if (StringUtils.isNotBlank(allCustsale.getExpert())){
                        String expertId = "";
                        List<String> experts = Arrays.asList(allCustsale.getExpert().split("、"));
                        for (String expert : experts) {
                            Expert expert1 = expertService.getOne(new QueryWrapper<Expert>().eq("name", expert));
                            if (expert1 == null) {
                                expert1 = new Expert();
                                expert1.setName(expert);
                                expertService.save(expert1);
                            }
                            expertId = expertId + "、" + expert1.getId();
                        }
                        projectResult.setExpertId(expertId);
                        projectResult.setExpertName(allCustsale.getExpert());
                    }

                    projectResultService.save(projectResult);
                }
                removeById(allCustsale.getId());
            }
        }
    }
}
